/******************************************************
 * 编写时间: 11_7_2021;
 * 编写人: 董书承 ;
 * 文件内容: 安装以及配置JDK环境
 ******************************************************
*/


# include "jdkIns.hh"
# include "status.hh"
# include "files.hh"
# include <string>
# include <fstream>
# include <iostream>
# include <cstdlib>
# include <vector>
# include <unistd.h>
# include <sys/types.h>
# include <map>
# include <cstdio>
# include <fstream>


std::string getSysVersion(void)
{

    std::system("mkdir ./temp/ 2>/dev/null ; lsb_release -a > ./temp/sysVer");
    std::vector <std::string> sysVerFile = vecFile("./temp/sysVer");
    std::string verstr;
    for(std::string chk: sysVerFile)
    {
        std::string rels = "Codename:  ";
        int inStr = chk.find("Codename");
        if (inStr != -1)
        {  
            verstr = chk.substr(inStr+rels.length()-1 , chk.length()-1 );

            break;
        }
        // std::cout << chk << std::endl;

    }
    std::cout << verstr << std::endl;

    return verstr;

}

int setupJDK(std::string version)
{
    // 1. 信任GPG公钥
    std::system("sudo wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -");
    // 2. 确定Ubuntu版本
    std::string codeN = getSysVersion();
    // 3. 写入软件源
    std::system(("sudo bash -c \" echo \\\" deb https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/deb " + codeN +" main \\\" > /etc/apt/sources.list.d/AdoptOpenJDK.list\"").c_str());
    // 4. 更新软件源
    std::system("sudo apt update");
    // 5. 下载安装具体版本的JDK
    std::system(("sudo apt install -y adoptopenjdk-"+ version +"-hotspot").c_str());
    // 6. 将当前版本的java javac 的优先级提到最大

    return SUCCESS;

}